
package Integral

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/Integral"
    IntegralReq "github.com/flipped-aurora/gin-vue-admin/server/model/Integral/request"
)

type IntegralsService struct {}
// CreateIntegrals 创建积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) CreateIntegrals(integrals *Integral.Integrals) (err error) {
	err = global.GVA_DB.Create(integrals).Error
	return err
}

// DeleteIntegrals 删除积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService)DeleteIntegrals(id string) (err error) {
	err = global.GVA_DB.Delete(&Integral.Integrals{},"id = ?",id).Error
	return err
}

// DeleteIntegralsByIds 批量删除积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService)DeleteIntegralsByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]Integral.Integrals{},"id in ?",ids).Error
	return err
}

// UpdateIntegrals 更新积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService)UpdateIntegrals(integrals Integral.Integrals) (err error) {
	err = global.GVA_DB.Model(&Integral.Integrals{}).Where("id = ?",integrals.Id).Updates(&integrals).Error
	return err
}

// GetIntegrals 根据id获取积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService)GetIntegrals(id string) (integrals Integral.Integrals, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&integrals).Error
	return
}
// GetIntegralsInfoList 分页获取积分表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService)GetIntegralsInfoList(info IntegralReq.IntegralsSearch) (list []Integral.Integrals, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&Integral.Integrals{})
    var integralss []Integral.Integrals
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&integralss).Error
	return  integralss, total, err
}
func (integralsService *IntegralsService)GetIntegralsPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
